#include <iostream>
#include <cstdlib>
#include <cassert>

using namespace std;

#include "Card.h"
#include "Deck.h"

Deck::Deck(){
	int index = 0;
	for (int suit=0; suit<4; suit++){
		for (int i=1; i<=13; i++){
			cards[index] = Card(i,Card::Suit(suit));
			index++;
			//cout << cards[i].toString();
		}
	}
	dealIndex = 0; //index of next available card
}

void Deck::shuffle(){
	//srand( time(NULL) );
    // Shuffle by exchanging each element randomly
    for (int i=0; i<(52-1); i++) {
        int randnum = i + (rand() % (52-i));
        cardSwap(cards[i], cards[randnum]);
    }
    dealIndex = 0;
}

void Deck::shuffle(bool randomize){
	if (randomize){
		srand( time(NULL) );
	}
    // Shuffle by exchanging each element randomly
    for (int i=0; i<(52-1); i++) {
        int randnum = i + (rand() % (52-i));
        cardSwap(cards[i], cards[randnum]);
    }
    dealIndex = 0;
}

Card Deck::dealCard(){
	return cards[dealIndex++];
}

int Deck::cardsRemaining() const{
	return (COUNT - dealIndex);
}

void Deck::cardSwap(Card & c1,Card & c2){
	Card tmp = c1;
	c1 = c2;
	c2 = tmp;
}